// Generate percentiles from CPS 2018 across IPUMS
if c(username)=="oficina" {
	cd "/Users/oficina/Dropbox (Personal)/skills_survey/Survey_adaptive/ReStat Replication and Publication Materials/data"
	}
else {
	loc user "`c(username)'"
	cd "C:\Users/`user'/Dropbox\skills_survey\Survey_adaptive\Survey Design\RandValues"
	}

u "IPUMS_2010_2018_hhincome.dta", clear
keep if year==2018
drop if pernum>1
duplicates tag cpsid serial, gen(tag)
assert tag==0
collapse (p10) p10=hhincome (p20) p20=hhincome (p25) p25=hhincome (p50) p50=hhincome (p75) p75=hhincome (p80) p80=hhincome (p90) p90=hhincome (firstnm) metarea [aw=asecwth], by(metfips)
gen dataset = "IPUMS CPS:2018 HH Income"
tempfile hold1
save `hold1'
save "../income quantiles.dta", replace


// Code to estimate the p10 and p90 at different IR levels

u "../income quantiles.dta", clear
drop if inlist(metfips,0,99998)
gen IR= p90/p10
gen IR_sq=IR^2
encode dataset, gen(ds_num)
// To see the distribution of the IIR
sum IR, d
keep if IR<100
// Estimating p10 and p90 for different IIR
foreach q in 10 90 {
	local name CPS
	regress p`q' c.IR##c.IR if IR<100 & ds_num==1
	predict p`q'_pred1 if e(sample)
	margins if e(sample), at((p10) IR) at((p20) IR) at((p30) IR) at((p40) IR) at((p50) IR) ///
		  at((p60) IR) at((p70) IR) at((p80) IR) at((p90) IR)
	mat define p`q'CPS = r(b)'
}

// Save database labeling as CPS
preserve
	keep metfips p?? IR dataset ds_num
	keep if ds_num==1
	renvars p??, postfix(CPS)
	rename IR incineqCPS
	keep metfips p??CPS incineqCPS
	tempfile cps
	save `cps'
restore

// Open CZ mobility data and generate values
preserve
	u "CZ_mobility.dta", clear
	gen prob_p1_k25 = 1-prob_p1_k1
	keep cz czname stateabbrv prob_p1_k25
	tempfile mobil
	save `mobil'
restore

// Get databases together
u `cps', clear
append using `mobil'
sum p50*
sum incineq*
sum prob*

// Generate values across percentiles
tempfile hold 
save `hold'
foreach v in p10CPS p20CPS p50CPS p80CPS p90CPS prob_p1_k25 incineqCPS {
	u `hold', clear
	egen quantile = xtile(`v'), nq(10)
	collapse (mean) `v', by(quantile)
	rename `v' mn10`v'
	replace quantile = 10*quantile
	tempfile hold2
	save `hold2' 
	
	u `hold', clear
	egen quantile = xtile(`v'), nq(5)
	collapse (mean) `v', by(quantile)
	rename `v' mn5`v'
	replace quantile = 20*quantile
	tempfile hold3
	save `hold3' 
	
	u `hold', clear
	pctile ptile`v' = `v', nq(10) gen(quantile)	
	keep ptile`v' quantile
	duplicates drop

	mer 1:1 quantile using `hold2', nogen
	mer 1:1 quantile using `hold3', nogen
	drop if quantile==. 
	tempfile sv`v'
	save `sv`v''
	}
u `svp10CPS', clear
mer 1:1 quantile using `svp20CPS', nogen
mer 1:1 quantile using `svp50CPS', nogen
mer 1:1 quantile using `svp80CPS', nogen
mer 1:1 quantile using `svp90CPS', nogen
mer 1:1 quantile using `svprob_p1_k25', nogen
mer 1:1 quantile using `svincineqCPS', nogen
order quantile
svmat p10CPS
svmat p90CPS
save "randomization_values.dta", replace

// Prepare format to export
u "randomization_values.dta", clear
keep quantile ptilep10CPS ptilep50CPS ptilep90CPS ptileprob_p1_k25  ptileincineq
drop in 10
drop quantile
rename ptilep10CPS p10income
rename ptilep90CPS p90income
rename ptilep50CPS p50income 
rename ptileprob_p1_k25 bottomupmob
gen mnincome = .2*p10income + .6*p50income + .2*p90income
drop mnincome ptileincineq
replace p10income = p10income/1000
replace p50income = p50income/1000
replace p90income = p90income/1000
format p10income %4.1f
format p50income %4.1f
format p90income %4.1f
gen pctile = _n

gen avgincome = .2*p10income + .6*p50income + .2*p90income

save "randomization_values.dta", replace

export delimited using "randomization_values.csv" , replace


*
* VALUES TO USE FOR WTP
*

// Values for p10, p50, p90 and bottom-up mobility
sum bottomupmob ,d
foreach v in p10income p50income p90income bottomupmob {
	// Regress variable against percentile
	reg `v' pctile
	}

foreach v in p10inc p90inc {
preserve
	keep `v'
	if "`v'"=="p10inc" {
		gen pctile = _n
	}
	else {
		gen pctile = _n*10
		}
	tempfile `v'
	save ``v''
restore
}
// Values for the income inequality ratio
preserve
	u `p10inc', clear
	cross using `p90inc'
	gen iir = p90inc/p10inc
	// Regress IIR against percentile
	reg iir pctile
	sum iir,d
restore


